WooCommerce: Get Order Info (total, items, etc) From $order Object

您所在的位置:网站首页 customer order number WooCommerce: Get Order Info (total, items, etc) From $order Object

WooCommerce: Get Order Info (total, items, etc) From $order Object

#WooCommerce: Get Order Info (total, items, etc) From $order Object| 来源: 网络整理| 查看: 265

As a WooCommerce development freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?“.

For example, “How can I get the order total“? Or “How can I get the order items“? Or maybe the order ID, customer ID, billing info, payment method, total refunds and so on… hopefully this article will help you save time as well!

As we’ve seen in other articles, get product info from $product object and get cart info from $cart object, not always you can have direct access to the $order variable.

Sometimes, you may have the $order_id available for example. In that scenario, you can “get” the order object with the wc_get_order WooCommerce function.

It’s also possible to gain $order information if you are in an email template. This can be helpful to show additional $order information in your transactional communications or trigger custom functions. Either way, enjoy!

1. You have access to $order variable

Hooks (do_action and apply_filters) use additional arguments which are passed on to the function. If they allow you to use the “$order” object you’re in business. Here’s how to get all the order information:

// Get Order ID and Key $order->get_id(); $order->get_order_key(); // Get Order Totals $0.00 $order->get_formatted_order_total(); $order->get_cart_tax(); $order->get_currency(); $order->get_discount_tax(); $order->get_discount_to_display(); $order->get_discount_total(); $order->get_total_fees(); $order->get_formatted_line_subtotal(); $order->get_shipping_tax(); $order->get_shipping_total(); $order->get_subtotal(); $order->get_subtotal_to_display(); $order->get_tax_location(); $order->get_tax_totals(); $order->get_taxes(); $order->get_total(); $order->get_total_discount(); $order->get_total_tax(); $order->get_total_refunded(); $order->get_total_tax_refunded(); $order->get_total_shipping_refunded(); $order->get_item_count_refunded(); $order->get_total_qty_refunded(); $order->get_qty_refunded_for_item(); $order->get_total_refunded_for_item(); $order->get_tax_refunded_for_item(); $order->get_total_tax_refunded_by_rate_id(); $order->get_remaining_refund_amount(); // Get and Loop Over Order Items foreach ( $order->get_items() as $item_id => $item ) { $product_id = $item->get_product_id(); $variation_id = $item->get_variation_id(); $product = $item->get_product(); // see link above to get $product info $product_name = $item->get_name(); $quantity = $item->get_quantity(); $subtotal = $item->get_subtotal(); $total = $item->get_total(); $tax = $item->get_subtotal_tax(); $tax_class = $item->get_tax_class(); $tax_status = $item->get_tax_status(); $allmeta = $item->get_meta_data(); $somemeta = $item->get_meta( '_whatever', true ); $item_type = $item->get_type(); // e.g. "line_item", "fee" } // Other Secondary Items Stuff $order->get_items_key(); $order->get_items_tax_classes(); $order->get_item_count(); $order->get_item_total(); $order->get_downloadable_items(); $order->get_coupon_codes(); // Get Order Lines $order->get_line_subtotal(); $order->get_line_tax(); $order->get_line_total(); // Get Order Shipping $order->get_shipping_method(); $order->get_shipping_methods(); $order->get_shipping_to_display(); // Get Order Dates $order->get_date_created(); $order->get_date_modified(); $order->get_date_completed(); $order->get_date_paid(); // Get Order User, Billing & Shipping Addresses $order->get_customer_id(); $order->get_user_id(); $order->get_user(); $order->get_customer_ip_address(); $order->get_customer_user_agent(); $order->get_created_via(); $order->get_customer_note(); $order->get_address_prop(); $order->get_billing_first_name(); $order->get_billing_last_name(); $order->get_billing_company(); $order->get_billing_address_1(); $order->get_billing_address_2(); $order->get_billing_city(); $order->get_billing_state(); $order->get_billing_postcode(); $order->get_billing_country(); $order->get_billing_email(); $order->get_billing_phone(); $order->get_shipping_first_name(); $order->get_shipping_last_name(); $order->get_shipping_company(); $order->get_shipping_address_1(); $order->get_shipping_address_2(); $order->get_shipping_city(); $order->get_shipping_state(); $order->get_shipping_postcode(); $order->get_shipping_country(); $order->get_address(); $order->get_shipping_address_map_url(); $order->get_formatted_billing_full_name(); $order->get_formatted_shipping_full_name(); $order->get_formatted_billing_address(); $order->get_formatted_shipping_address(); // Get Order Payment Details $order->get_payment_method(); $order->get_payment_method_title(); $order->get_transaction_id(); // Get Order URLs $order->get_checkout_payment_url(); $order->get_checkout_order_received_url(); $order->get_cancel_order_url(); $order->get_cancel_order_url_raw(); $order->get_cancel_endpoint(); $order->get_view_order_url(); $order->get_edit_order_url(); // Get Order Status $order->get_status(); // Get Thank You Page URL $order->get_checkout_order_received_url(); 2. You have access to $order_id variable

If you have access to the order ID (once again, usually the do_action or apply_filters might give you this), you have to get the order object first. Then do the exact same things as above.

// Get $order object from order ID $order = wc_get_order( $order_id ); // Now you have access to (see above)... if ( $order ) { $order->get_formatted_order_total( ); // etc. // etc. } 3. You have access to $email variable

If you are working with WooCommerce emails, often you will have the $email object available as parameter. In order to get the object from that, you need an additional step. Then do the exact same things as above.

// Get $order object from $email $order = $email->object; // Now you have access to (see above)... if ( $order ) { $order->get_id(); $order->get_formatted_order_total( ); // etc. // etc. }

Related posts: WooCommerce: Advanced Order Search WooCommerce Database Explained: How It Works and Where To Find Data WooCommerce: How To Print Orders Automatically WooCommerce: File Upload Plugins For Order Personalization WooCommerce: How to Shop As a Customer


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3